package com.bnz.service.impl;

import com.bnz.dao.StudentDao;
import com.bnz.entity.Student;
import com.bnz.service.StudentService;
import com.bnz.utils.MyBatisUtil;
import com.bnz.vo.StuVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    public List<Student> findAll() {
        try (SqlSession session = MyBatisUtil.getSqlSession()) {
            //1.1 查询所有学生列表
            StudentDao dao = session.getMapper(StudentDao.class);

            List<Student> students = dao.findAll();
            //1.2 修改某个学生所在班级

            return students;
        }
    }

    @Override
    public int deleteById(int id) {
        try (SqlSession session = MyBatisUtil.getSqlSession()) {

            StudentDao dao = session.getMapper(StudentDao.class);

            int flag = dao.deleteById(id);


            return flag;
        }
    }

    @Override
    public int modifyById(Student student) {
        try (SqlSession session = MyBatisUtil.getSqlSession()) {

            StudentDao dao = session.getMapper(StudentDao.class);

            int flag = dao.modifyById(student);


            return flag;
        }
    }

    @Override
    public int addStudent(Student student) {
        try (SqlSession session = MyBatisUtil.getSqlSession()) {

            StudentDao dao = session.getMapper(StudentDao.class);
            int flag = dao.addStudent(student);

            return flag;
        }
    }

    @Override
    public Student queryById(int id) {
        try (SqlSession session = MyBatisUtil.getSqlSession()) {

            StudentDao dao = session.getMapper(StudentDao.class);

            Student student = dao.queryById(id);

            return student;
        }
    }

    @Override
    public PageInfo<Student> queryByPage(StuVo student , int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        try (SqlSession session = MyBatisUtil.getSqlSession()) {

            StudentDao dao = session.getMapper(StudentDao.class);

            List<Student> list = dao.byPage(student);
            PageInfo<Student> info = new PageInfo<>(list);
            return info;
        }
    }
}
